package cn.iocoder.yudao.module.crm.service.performance.config;

import cn.iocoder.yudao.module.crm.controller.admin.performance.config.vo.PerformanceConfigPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.performance.config.vo.PerformanceConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.performance.config.PerformanceConfigDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.performance.config.PerformanceConfigDetailDO;
import cn.iocoder.yudao.module.crm.dal.mysql.performance.config.PerformanceConfigDetailMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.performance.config.PerformanceConfigMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;

/**
 * CRM_业绩配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PerformanceConfigServiceImpl implements PerformanceConfigService {

    @Resource
    private PerformanceConfigMapper performanceConfigMapper;

    @Resource
    private PerformanceConfigDetailMapper performanceConfigDetailMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPerformanceConfig(PerformanceConfigSaveReqVO createReqVO) {
        Long[] deptIds = createReqVO.getDeptIdS();
        for (Long deptId : deptIds) {
            // 先删除同年同部门的数据 保持一个部门一个年份只有一条业绩指标配置
            Map<String,Object> deleteMap = new HashMap<>();
            deleteMap.put("dept_id",deptId);
            deleteMap.put("year",createReqVO.getYear());
            deleteMap.put("performance_point", createReqVO.getPerformancePoint());
            performanceConfigMapper.deleteByMap(deleteMap);
            QueryWrapper<PerformanceConfigDetailDO> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("dept_id", createReqVO.getPerformancePoint());
            deleteWrapper.likeLeft("belong_month",createReqVO.getYear());
            deleteWrapper.eq("performance_point", createReqVO.getPerformancePoint());
            performanceConfigDetailMapper.delete(deleteWrapper);// 删除同年份 同部门 同指标点的数据
            // 插入
            PerformanceConfigDO performanceConfig = BeanUtils.toBean(createReqVO, PerformanceConfigDO.class);
            performanceConfig.setDeptId(deptId);
            performanceConfigMapper.insert(performanceConfig);
            List<PerformanceConfigDetailDO> list = createDetailListFromReqVO(createReqVO,deptId);
            performanceConfigDetailMapper.insertBatch(list);
        }



        // 返回
//        return performanceConfig.getId();
    }

    private List<PerformanceConfigDetailDO> createDetailListFromReqVO(PerformanceConfigSaveReqVO createReqVO, Long deptId) {
        List<PerformanceConfigDetailDO> list = new ArrayList<>();

        PerformanceConfigDetailDO monthDetail01 = new PerformanceConfigDetailDO();
        monthDetail01.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail01.setBelongMonth(createReqVO.getYear()+"-01");
        monthDetail01.setDeptId(deptId);
        monthDetail01.setTargetValue(createReqVO.getTargetValue1());
        list.add(monthDetail01);

        PerformanceConfigDetailDO monthDetail02 = new PerformanceConfigDetailDO();
        monthDetail02.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail02.setBelongMonth(createReqVO.getYear()+"-02");
        monthDetail02.setDeptId(deptId);
        monthDetail02.setTargetValue(createReqVO.getTargetValue2());
        list.add(monthDetail02);

        PerformanceConfigDetailDO monthDetail03 = new PerformanceConfigDetailDO();
        monthDetail03.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail03.setBelongMonth(createReqVO.getYear()+"-03");
        monthDetail03.setDeptId(deptId);
        monthDetail03.setTargetValue(createReqVO.getTargetValue3());
        list.add(monthDetail03);

        PerformanceConfigDetailDO monthDetail04 = new PerformanceConfigDetailDO();
        monthDetail04.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail04.setBelongMonth(createReqVO.getYear()+"-04");
        monthDetail04.setDeptId(deptId);
        monthDetail04.setTargetValue(createReqVO.getTargetValue4());
        list.add(monthDetail04);

        PerformanceConfigDetailDO monthDetail05 = new PerformanceConfigDetailDO();
        monthDetail05.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail05.setBelongMonth(createReqVO.getYear()+"-05");
        monthDetail05.setDeptId(deptId);
        monthDetail05.setTargetValue(createReqVO.getTargetValue5());
        list.add(monthDetail05);

        PerformanceConfigDetailDO monthDetail06 = new PerformanceConfigDetailDO();
        monthDetail06.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail06.setBelongMonth(createReqVO.getYear()+"-06");
        monthDetail06.setDeptId(deptId);
        monthDetail06.setTargetValue(createReqVO.getTargetValue6());
        list.add(monthDetail06);

        PerformanceConfigDetailDO monthDetail07 = new PerformanceConfigDetailDO();
        monthDetail07.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail07.setBelongMonth(createReqVO.getYear()+"-07");
        monthDetail07.setDeptId(deptId);
        monthDetail07.setTargetValue(createReqVO.getTargetValue7());
        list.add(monthDetail07);

        PerformanceConfigDetailDO monthDetail08 = new PerformanceConfigDetailDO();
        monthDetail08.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail08.setBelongMonth(createReqVO.getYear()+"-08");
        monthDetail08.setDeptId(deptId);
        monthDetail08.setTargetValue(createReqVO.getTargetValue8());
        list.add(monthDetail08);

        PerformanceConfigDetailDO monthDetail09 = new PerformanceConfigDetailDO();
        monthDetail09.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail09.setBelongMonth(createReqVO.getYear()+"-09");
        monthDetail09.setDeptId(deptId);
        monthDetail09.setTargetValue(createReqVO.getTargetValue9());
        list.add(monthDetail09);

        PerformanceConfigDetailDO monthDetail10 = new PerformanceConfigDetailDO();
        monthDetail10.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail10.setBelongMonth(createReqVO.getYear()+"-10");
        monthDetail10.setDeptId(deptId);
        monthDetail10.setTargetValue(createReqVO.getTargetValue10());
        list.add(monthDetail10);

        PerformanceConfigDetailDO monthDetail11 = new PerformanceConfigDetailDO();
        monthDetail11.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail11.setBelongMonth(createReqVO.getYear()+"-11");
        monthDetail11.setDeptId(deptId);
        monthDetail11.setTargetValue(createReqVO.getTargetValue11());
        list.add(monthDetail11);

        PerformanceConfigDetailDO monthDetail12 = new PerformanceConfigDetailDO();
        monthDetail12.setPerformancePoint(createReqVO.getPerformancePoint());
        monthDetail12.setBelongMonth(createReqVO.getYear()+"-12");
        monthDetail12.setDeptId(deptId);
        monthDetail12.setTargetValue(createReqVO.getTargetValue12());
        list.add(monthDetail12);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePerformanceConfig(PerformanceConfigSaveReqVO updateReqVO) {
        // 校验存在
//        validatePerformanceConfigExists(updateReqVO.getId());
        PerformanceConfigDO oldData = getPerformanceConfig(updateReqVO.getId());
        if (oldData == null) {
            throw exception(PERFORMANCE_CONFIG_NOT_EXISTS);
        }else{
            // 校验冲突
            validatePerformanceConfigConflict(updateReqVO);
            // 更新
            PerformanceConfigDO updateObj = BeanUtils.toBean(updateReqVO, PerformanceConfigDO.class);
            performanceConfigMapper.updateById(updateObj);
            //删除明细表的旧数据 插入新数据
            QueryWrapper<PerformanceConfigDetailDO> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("dept_id", oldData.getDeptId());
            deleteWrapper.likeLeft("belong_month",oldData.getYear());
            deleteWrapper.eq("performance_point", oldData.getPerformancePoint());
            performanceConfigDetailMapper.delete(deleteWrapper);// 删除同年份 同部门 同指标点的数据
            List<PerformanceConfigDetailDO> list = createDetailListFromReqVO(updateReqVO, updateReqVO.getDeptId());
            performanceConfigDetailMapper.insertBatch(list);
        }


    }

    /**
     * 校验冲突
     * @param updateReqVO
     */
    private void validatePerformanceConfigConflict(PerformanceConfigSaveReqVO updateReqVO){
        QueryWrapper<PerformanceConfigDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("performance_point",updateReqVO.getPerformancePoint());
        queryWrapper.eq("year",updateReqVO.getYear());
        queryWrapper.eq("dept_id",updateReqVO.getDeptId());
        queryWrapper.ne("id",updateReqVO.getId());
        List<PerformanceConfigDO> list = performanceConfigMapper.selectList(queryWrapper);
        if(!list.isEmpty()){
            throw exception(PERFORMANCE_CONFIG_CONFLICT);
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePerformanceConfig(Long id) {
        // 校验存在
        validatePerformanceConfigExists(id);
        PerformanceConfigDO oldData = performanceConfigMapper.selectById(id);
        QueryWrapper<PerformanceConfigDetailDO> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("dept_id", oldData.getDeptId());
        deleteWrapper.likeLeft("belong_month",oldData.getYear());
        deleteWrapper.eq("performance_point", oldData.getPerformancePoint());
        performanceConfigDetailMapper.delete(deleteWrapper);// 删除同年份 同部门 同指标点的数据
        // 删除
        performanceConfigMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePerformanceConfigListByIds(List<Long> ids) {
        // 删除
        List<PerformanceConfigDO> oldDataList = performanceConfigMapper.selectByIds(ids);
        for(PerformanceConfigDO oldData : oldDataList){
            QueryWrapper<PerformanceConfigDetailDO> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("dept_id", oldData.getDeptId());
            deleteWrapper.likeLeft("belong_month",oldData.getYear());
            deleteWrapper.eq("performance_point", oldData.getPerformancePoint());
            performanceConfigDetailMapper.delete(deleteWrapper);// 删除同年份 同部门 同指标点的数据
        }
            performanceConfigMapper.deleteByIds(ids);
    }


    private void validatePerformanceConfigExists(Long id) {
        if (performanceConfigMapper.selectById(id) == null) {
            throw exception(PERFORMANCE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public PerformanceConfigDO getPerformanceConfig(Long id) {
        return performanceConfigMapper.selectById(id);
    }

    @Override
    public PageResult<PerformanceConfigDO> getPerformanceConfigPage(PerformanceConfigPageReqVO pageReqVO) {
        return performanceConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PerformanceConfigDO> getPerformanceConfigResultList(PerformanceConfigPageReqVO pageReqVO) {
        performanceConfigMapper.selectList(pageReqVO);
        return Collections.emptyList();
    }

}